import {Injectable, Component} from "@angular/core";
import {NativeTransitionOptions} from "@ionic-native/native-page-transitions";
import { AlertController, Platform } from 'ionic-angular';
import { text } from "@angular/core/src/render3/instructions";
import { File } from "@ionic-native/file";



@Injectable()
export class Base {
  // BASE_URL = "http://39.108.184.47:8081/"
  // BASE_URL = "http://192.168.1.2:50000/"
  // BASE_URL = "http://106.15.200.245:50000/"
    // BASE_URL = "http://106.15.90.78:50000/"
  //   BASE_URL = "http://192.168.1.7:50000/"
  //BASE_URL = "http://127.0.0.1:50000/"
  // BASE_URL = "http://118.178.94.5:50000/"
    BASE_URL = "http://183.250.217.175:3389/"
  transitionOptions: NativeTransitionOptions = {
    direction: 'left',
    duration: 200,
    slowdownfactor: 3,
    slidePixels: 20,
    iosdelay: 0,
    androiddelay: 0,
    fixedPixelsTop: 0,
    fixedPixelsBottom: 60
  };
  fileBasePath:any;
  constructor(private alertCtrl: AlertController,
    private file:File,public platform:Platform) {
      if(this.platform.is('android')){
        this.fileBasePath = this.file.externalDataDirectory;
      }else{
        this.fileBasePath = this.file.documentsDirectory;
      }
  }


  showAlert(title, content, func) {
    const alert = this.alertCtrl.create({
      title: title,
      subTitle: content,
      buttons: [{text:'确认', handler: func}]
    });
    alert.present();
  }

  readLogger(filename):string{
    this.file.checkFile(this.fileBasePath,filename).then((success)=>{
      this.file.readAsText(this.fileBasePath,filename).then((success)=>{
        console.log("读取成功");
        return success;
      });
    },(err)=>{
      console.log("文件不存在");
      console.log(err);
      return null;
    })
    return null;
  }

  logger(info:string,storage:string){
    return new Promise((resovle,reject)=>{
      var that = this;
      this.file.checkFile(this.fileBasePath,storage).then(function (success){
         
        that.file.writeFile(that.fileBasePath, storage, '[' + info + '],', { append: true }).then(function (success) {
          resovle('ok' + JSON.stringify(success));
          // success
        },function (err){
          reject('error' + JSON.stringify(err));
        });
      },function(err){
         
        that.file.writeFile(that.fileBasePath, storage, '[' + info + '],', { replace: true }).then(function (success) {
          resovle('ok' + JSON.stringify(success));
            // success
          },function (err){
            reject('error' + JSON.stringify(err));
          });
  
      });
    })

  }

  loggerReplaceMode(info:string,storage:string){
    this.file.writeFile(this.fileBasePath, storage, '' + info + '' , { replace: true }).then(function (success) {
      console.log("写入成功");
      // success
    }, function (error) {
      console.log("写入失败");
      console.log(error);
      alert("写入缓存文件出错! 错误信息:" + error);
      // error
    });
  }

  convertToBase64(path,osVal){
    if(osVal == 1){
      this.file.readAsDataURL(this.file.tempDirectory,path).then((imageBase64)=>{
         

        return imageBase64;
      })
    }else{
      this.file.readAsDataURL(this.file.externalDataDirectory, path).then((imageBase64) => {
         
        return imageBase64;
      })
    }

  }

  showPrompt(title, myName,func1, func2) {
    const prompt = this.alertCtrl.create({
      title: title,
      inputs: [
        {
          type:'text',
          name: myName,
          placeholder: '设备id'
        }

      ],
      buttons: [
        {
          text: '取消',
          handler: func2
        },
        {
          text: '确认',
          handler: (data)=>{
            func1(data);
          }
        }
      ]
    });
    prompt.present();
  }


  showConfirmAlert(title, content, func1, func2) {
    const confirm = this.alertCtrl.create({
      title: title,
      message: content,
      buttons: [
        {
          text: '取消',
          handler: func2
        },
        {
          text: '确认',
          handler: ()=>{
            func1(confirm);
          }
        }
      ]
    });
    confirm.present();
  }

  static popTo(navCtrl, name) {
    let views = navCtrl.getViews();
    let target = -1;
    for (let i = 0; i < views.length; ++i) {
      if (views[i].name == name) {
        target = i;
        break;
      }
    }
    if (target >= 0) {
      navCtrl.popTo(navCtrl.getByIndex(target)).then(()=>{});
    }

  }
}
